﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Nomenclature : IEquatable<Nomenclature>
    {
        public void ImportList(List<Classes.Nomenclature> nomenList)
        {
            var mdb = new MedregDBDataContext ();
            var ListNomenclature = new List<Nomenclature>();
            foreach (var nomen in nomenList)
            {
                Nomenclature nomen1 = new Nomenclature {ID = nomen.ID, Name = nomen.Name, Parent = nomen.Parent};
                Nomenclature query;

                if (mdb.Nomenclature.LongCount() == 0)
                {
                    mdb.Nomenclature.InsertOnSubmit(nomen1);
                }
                else
                {
                    try
                    {
                        query = mdb.Nomenclature.Single(l => l.ID == nomen1.ID);
                        query.Name = nomen1.Name;
                        query.Parent = nomen1.Parent;
                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        
                        if (!ListNomenclature.Contains(nomen1))
                        {
                            mdb.Nomenclature.InsertOnSubmit(nomen1);
                            ListNomenclature.Add(nomen1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Nomenclature other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
        
    }
}
